import {request, writeLog} from "@/libs/core.js"
import { Message } from 'element-ui'
const getTree = async(args) => {
    const data = await request({url:'/api/core/module/all', data:args, method:'post'});
    if (data && data.code == 0 && data.datas.tree) {
        return data.datas.tree;
    }
    return [];
}

const getNode = async(args) => {
    const data = await request({url:'/api/core/module/node', data:args, method:'post'});
    if (data && data.code == 0) {
        if (data.datas.nodes) {
            for (let idx = 0; idx < data.datas.nodes.length; idx++) {
                data.datas.nodes[idx].leaf = true;
            }
        }
        return data.datas.nodes;
    }
    return [];
}

const doDelete = async(args) => {
	const data = await request({url:'/api/core/module/delete', data:args, method:'post'});
	if(data && data.code == 0){
		Message({message: '权限模块删除成功', type: 'success', duration: 3 * 1000});
		return true;
	}
    return false;
}

const doSave = async(args) => {
	const data = await request({url:'/api/core/module/update', data:args, method:'post'});
	if(data && data.code == 0){
		if(!args.id){
			Message({message: '权限模块新增成功', type: 'success', duration: 3 * 1000});
		}else{
			Message({message: '权限模块编辑成功', type: 'success', duration: 3 * 1000});
		}
		return true;
	}
    return false;
}

export default {
	getTree, getNode, doDelete, doSave
};
